package com.agent.diagnostic;

import java.util.ArrayList;
import java.util.List;

import com.agent.diagnostic.AbtTrace;
import com.agent.diagnostic.IOutputWriter;
import com.agent.diagnostic.MessageLog;
import com.agent.diagnostic.TraceConsole;
import com.agent.diagnostic.TraceFile;


class TraceConstant{
	public final static int FILE_MODE = 1;
	public final static int CONSOLE_MODE = 2;
}

/*
 * class Diagnostic : it provide 3 main method
 * logError, logWarning and logInfor
 */
public class AbtTrace {
	 private static IOutputWriter m_taLogger = null;
	 private static List<IOutputWriter> m_lTraceOutPut = new ArrayList<IOutputWriter>(0);
	 private static TraceLevel traceLevel = TraceLevel.INFO;
	 /*
	  * initialize environment for diagnostic
	  */
	 static
	 {
		 String sDebugMode = System.getenv("TADEBUG_MODE");
		 if(sDebugMode!= null && sDebugMode.equals(String.valueOf(TraceConstant.FILE_MODE))){
			 m_taLogger = new TraceFile();
		 }else if(sDebugMode!= null && sDebugMode.equals(String.valueOf(TraceConstant.CONSOLE_MODE))){
			 m_taLogger = new TraceConsole();
		 }
		 if(m_taLogger != null)
			 m_lTraceOutPut.add(m_taLogger);
	 }
	
	 public static void setTraceLevel(TraceLevel traceLevel){
		 AbtTrace.traceLevel=traceLevel;	
	 }
	 
	 /*
	  *	print error log 
	  */
	 public static void error(String msg){
		 printMsg(msg, TraceLevel.ERROR);
	 }
	 
	 private static void printMsg(String msg, TraceLevel traceLevel){
		 if(doesSkip(traceLevel)){
			 return ;
		 }
		 MessageLog msglog= new MessageLog(msg,traceLevel);
		 for(IOutputWriter trace : m_lTraceOutPut ){
			 trace.write(msglog.getMessage(),traceLevel);
		 }
	 }
	 
	 /*
	  * print information log
	  */
	 public static void info(String msg){
		 printMsg(msg, TraceLevel.INFO);
	 }
	 
	 /*
	  * print warning log
	  */
	 public static void warning(String msg){
		 printMsg(msg, TraceLevel.WARNING);
	 }
	 
	 /**
	  * check if it should skip this message mode
	  * for instance: there is not any output writer or message is invalid
	  * @param msgMode
	  * @return
	  */
	 private static boolean doesSkip(TraceLevel msgMode){
		
		 if(m_lTraceOutPut==null || m_lTraceOutPut.isEmpty()){
			 return true;
		 }
		 if(AbtTrace.traceLevel.getLevel() >msgMode.getLevel() ){
			return true; 
		 }
		 return false;
		 
	 }
	 
	 
	
	 /*
	  * print if condition true
	  */
	 public static void warningIf(boolean bCondition,String msg){
		 
		 if(bCondition)
			 warning(msg);
	 }
	 
	 /*
	  * print if condition true
	  */
	 public static void inforIf(boolean bCondition,String msg){
		 
		 if(bCondition)
			 info(msg);
	 }
	 
	 /*
	  * print if condition true
	  */
	 public static void errorIf(boolean bCondition,String msg){
	 
		 if(bCondition)
			 error(msg);
	 }
	
	 /*
	  * add trace to trace list when want use many trace
	  */
	 public static void addOutputWriter(IOutputWriter outPut){
		 if(outPut != null)
			 m_lTraceOutPut.add(outPut);
	 }
	 
	 public static TraceLevel getTraceLevel(){
		 return traceLevel;
	 }
	 
}
